/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "node/exception.h"
#include "node/base/console.h"
#include "node/system/thread.h"
using ::node::base::Console;
using ::node::system::Thread;

class TestWorker : public Thread
{
public:
    TestWorker(int id)
        : id_(id)
        , counter_(-1)
    {
    }
    ~TestWorker(void)
    {

    }

    virtual void Stop(void)
    {

    }
protected:
    virtual void Run(void)
    {
        try {
            while (1)
            {
                Console::Info("counter %d is %d\n", id_, ++counter_);
                Thread::Sleep(1000);

                if (counter_ > 3) {
                    throw ::node::LogicException();
                }
            }
        } catch (::node::Exception& e) {
            Console::Error("e, %d, %s\n", e.code(), e.what());
        }
    }

private:
    int id_;
    int counter_;
};

int main(int argc, char *argv[])
{
    Console::Info("program is %s\n", argv[0]);
    Console::Warning("program is %s\n", argv[0]);
    Console::Error("program is %s\n", argv[0]);
    Console::Info("program is %s\n", argv[0]);

    TestWorker worker1(1), worker2(2), worker3(3);
    worker1.Start();
    worker2.Start();
    worker3.Start();

    worker1.Join();
    worker2.Join();
    worker3.Join();
    return 0;
}